package com.atguigu.dao.impl;

import com.atguigu.dao.EmployeeDao;
import com.atguigu.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/23 20:43
 * @注释
 */
@Repository
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Employee> findAllDao() {
        String sql = "select * from t_employee";
        List<Employee> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class));
        return query;
    }

    @Override
    public Employee findEmployeeById(Integer id) {
        String sql = "select * from t_employee where id = ?";
        List<Employee> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class), id);
        return query.get(0);
    }

    @Override
    public String addEmployee(Employee employee) {
        String sql = "insert into t_employee values(null,?,?,?,?)";
        jdbcTemplate.update(sql, employee.getName(), employee.getAge(), employee.getGender(), employee.getEmail());
        return "success";
    }

    @Override
    public String updateEmployee(Employee employee) {
        String sql = "UPDATE t_employee SET `name` = ?, age = ?, gender = ?, email = ? WHERE id = ?";
        jdbcTemplate.update(sql, employee.getName(), employee.getAge(), employee.getGender(), employee.getEmail(), employee.getId());
        return "success";
    }

    @Override
    public String deleteEmployee(Integer id) {
        String sql = "delete from t_employee where id = ?";
        jdbcTemplate.update(sql, id);
        return "success";
    }
}
